package common.spring.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;


import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * StringRedisUnits采用本地序列化策略，存取字符串时建议用StringRedisUnits
 */
public class StringRedisUnits extends StringRedisTemplate {

    protected  StringRedisTemplate stringRedisTemplate;
    public StringRedisUnits() {
        stringRedisTemplate=this;
    }

    public StringRedisUnits(RedisConnectionFactory connectionFactory) {
        this();
        this.setConnectionFactory(connectionFactory);
        this.afterPropertiesSet();
    }

    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }

    private final Logger LOG = LoggerFactory.getLogger(StringRedisUnits.class);



    private boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            String str = obj.toString();
            if ("".equals(str.trim())) {
                return true;
            }
            return false;
        }
        if (obj instanceof List) {
            List<Object> list = (List<Object>) obj;
            if (list.isEmpty()) {
                return true;
            }
            return false;
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            if (map.isEmpty()) {
                return true;
            }
            return false;
        }
        if (obj instanceof Set) {
            Set set = (Set) obj;
            if (set.isEmpty()) {
                return true;
            }
            return false;
        }
        if (obj instanceof Object[]) {
            Object[] objs = (Object[]) obj;
            if (objs.length <= 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 缓存存入key-value
     *
     * @param key   缓存键
     * @param value 缓存值
     * @return true:成功 false:失败
     */
    public boolean setString(String key, String value) {
        LOG.debug(" setString key :{}, value: {}", key, value);
        try {
            if ( isEmpty(key) || isEmpty(value)) {
                return false;
            }
            stringRedisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
    }

    /**
     * 缓存存入key-value
     *
     * @param key     缓存键
     * @param value   缓存值
     * @param seconds 秒数
     * @return true:成功 false:失败
     */
    public boolean setString(String key, String value, long seconds) {
        LOG.debug(" setString key :{}, value: {}, timeout:{}", key, value, seconds);
        try {
            if (isEmpty(key) || isEmpty(value)) {
                return false;
            }
            stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
    }

    public boolean setStringDay(String key, String value, long day) {
        LOG.debug(" setString key :{}, value: {}, timeout:{}", key, value, day);
        try {
            if (isEmpty(key) || isEmpty(value)) {
                return false;
            }
            stringRedisTemplate.opsForValue().set(key, value, day, TimeUnit.DAYS);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
    }

    /**
     * 根据key取出String value
     *
     * @param key 缓存key值
     * @return String    缓存的String
     */
    public String getString(String key) {
        LOG.debug(" getString key :{}", key);
        try {
            if (isEmpty(key)) {
                return null;
            }
            return stringRedisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }



}
